<template>
  <div>
    <!-- 返利明细查询 -->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @resetChange="resetChange" @onCollapse="collapseHandle" :labelWidth="110"></TopFilter>

    <Spin :spinning="exportLoading" tip="Loading...">
      <FilterTable
        ref="table"
        columnsRef="qjhRebateDetailsQuery"
        :isSelectColumn="false"
        :columns="columns"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :datakey="'records'"
        :onColumnsChange="columns => (this.columns = columns)"
      >
        <template slot="controls">
          <multiuse-button size="small" type="primary" :click="exportHandler">导 出</multiuse-button>
        </template>
      </FilterTable>
    </Spin>

    <!-- 经销商查询 -->
    <BaseDialog :visible.sync="isDealer" title="经销商查询" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <OwnerSearch :rowData="formData" @close="emitCloseHandler" />
    </BaseDialog>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { getRegion } from '@/api/omFinance';
import { queryList, exportExcel, getFundsType, queryRebateSubprojects ,exporturl} from '@/api/omFinance/RebateManage/RebateDetailsQuery';
import { exportExcelFile } from '@/utils/index';
import OwnerSearch from '../OwnerSearch';

export default {
  name: 'RebateDetailsQuery',
  mixins: [dictionary],

  components: {
    OwnerSearch
  },
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchApi: queryList,
      fetchParams: {},
      formData: {},
      isDealer: false,
      exportLoading: false
    };
  },

  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
    this.getFundsType();
    this.getRebateSubprojects();
    // this.getDeptData();
  },
  methods: {
    seacherFun() {
      this.isDealer = true;
    },
    emitCloseHandler(val, data) {
      this.isDealer = val;
      let dealerCodes = [];
      let dealerNames = [];
      data.map(x => {
        dealerCodes.push(x.dealerCode);
        dealerNames.push(x.dealerName);
      });
      this.topFilterList.find(x => x.fieldName == 'dealerCode').initialValue = dealerCodes.toString();
    },
    changeHandle(params) {
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    resetChange(params) {
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    // 账户资金类型
    async getFundsType(powerType) {
      const res = (await getFundsType({ powerType: powerType, fundsType: 1 })).data.map(x => ({ text: x.accountTypeName, value: x.id }));
      this.topFilterList.find(x => x.fieldName == 'accountTypeId').itemList = res;
    },
    // 返利子项目
    async getRebateSubprojects(id) {
      const res = (await queryRebateSubprojects({ accountTypeId: id })).data.map(x => ({ text: x.rebateSubprojectsName, value: x.id }));
      this.topFilterList.find(x => x.fieldName == 'rebateSubprojectsId').itemList = res;
    },
    // 加载事业部数据
    // async getDeptData() {
    //   const res = await getRegion();
    //   if (res.resultCode === 200) {
    //     res.data.map(x => {
    //       this.topFilterList.find(item => item.fieldName == 'deptId').itemList.push({ text: x.orgName, value: x.id });
    //     });
    //   }
    // },
    // 导出
    async exportHandler() {

  let json = Object.assign({}, { CODE: 'CODE_0000005' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }


      // await this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
      //   confirmButtonText: '下载',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // })
      //   .then(() => {
      //     this.exportLoading = true;
      //     exportExcel(this.fetchParams)
      //       .then(response => {
      //         exportExcelFile(response.data, '返利明细查询');
      //         this.exportLoading = false;
      //       })
      //       .catch(err => {
      //         console.log(err);
      //       });
      //   })
      //   .catch(() => {
      //     this.$message({
      //       type: 'info',
      //       message: '已取消'
      //     });
      //   });
    },
    createdFrom() {
      return [
        // {
        //   type: 'SELECT',
        //   label: '事业部',
        //   fieldName: 'deptId',
        //   placeholder: '请选择',
        //   itemList: []
        // },
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCode',
          placeholder: '请选择',
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun()}></el-button>
              </div>
            );
          }
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          placeholder: '请输入'
        },

        {
          type: 'SELECT',
          label: '动力形式',
          fieldName: 'powerType',
          placeholder: '请选择',
          itemList: this.createDictList('6008'),
          change: val => {
            if (val) {
              this.topFilterList.map(x => {
                if (x.fieldName == 'accountTypeId') {
                  x.initialValue = '';
                  x.itemList = [];
                }
              });
              this.getFundsType(val);
              return;
            }
            this.topFilterList.map(x => {
              if (x.fieldName == 'accountTypeId') {
                x.initialValue = '';
                x.itemList = [];
              }
            });
          }
        },
        {
          type: 'INPUT',
          label: '返利名称',
          fieldName: 'rebateName',
          placeholder: '请选择'
        },
        {
          type: 'SELECT',
          label: '返利账户类型',
          fieldName: 'accountTypeId',
          placeholder: '请选择',
          itemList: [],
          change: val => {
            if (val) {
              this.topFilterList.find(x => x.fieldName == 'rebateSubprojectsId').initialValue = '';
              this.topFilterList.find(x => x.fieldName == 'rebateSubprojectsId').itemList = [];
              this.getRebateSubprojects(val);
              return;
            }
            this.topFilterList.find(x => x.fieldName == 'rebateSubprojectsId').initialValue = '';
            this.topFilterList.find(x => x.fieldName == 'rebateSubprojectsId').itemList = [];
            this.getRebateSubprojects();
          }
        },
        {
          type: 'SELECT',
          label: '返利子项目',
          fieldName: 'rebateSubprojectsId',
          placeholder: '请选择',
          itemList: []
        },
        {
          type: 'SELECT',
          label: '业务类型',
          fieldName: 'businessType',
          placeholder: '请选择',
          itemList: this.createDictList('6045')
        },
        {
          type: 'INPUT',
          label: '业务单据号',
          fieldName: 'billNo',
          placeholder: '请选择'
        },
        {
          type: 'RANGE_DATE',
          label: '记账时间范围',
          dateType: 'exactdaterange',
          fieldName: 'accountDateStartDate|accountDateEndDate',
          placeholder: '请选择'
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '事业部',
          dataIndex: 'deptName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商代码',
          dataIndex: 'dealerCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: 'SAP业务单据号',
          dataIndex: 'sapBillNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '返利账户类型',
          dataIndex: 'rebateTypeName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '返利子项目',
          dataIndex: 'rebateSubprojectsName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '记账时间',
          dataIndex: 'accountDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '业务单据号',
          dataIndex: 'billNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '业务类型',
          dataIndex: 'businessType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6045'),
          render: props => {
            return <span>{this.createDictText(props.row.businessType, '6045')}</span>;
          }
        },
        {
          title: '业务操作',
          dataIndex: 'detailRemark',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '原金额',
          dataIndex: 'originalAmount',
          sorter: true,
          filter: true,
          filterType: 'input',
          summation: true,
          precision: 2,
          numberFormat: true
        },
        {
          title: '发生金额',
          dataIndex: 'chngAmount',
          sorter: true,
          filter: true,
          filterType: 'input',
          summation: true,
          precision: 2,
          numberFormat: true
        },
        {
          title: '账户余额',
          dataIndex: 'balanceAmount',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true
        },
        {
          title: '返利名称',
          dataIndex: 'rebateName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '返利归属年份',
          dataIndex: 'rebateYear',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '备注',
          dataIndex: 'remark',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    }
  }
};
</script>
